﻿//-----------------------------------------------------------------------------
// <copyright file="Deck.cs" company="Company">
//  What does one write here?
// </copyright>
//-----------------------------------------------------------------------------

namespace Cards
{
    using System;

    /// <summary>
    /// A class representing a standard deck of cards
    /// </summary>
    public sealed class Deck
    {
        /// <summary>
        /// The number of cards in a full deck (13 ranks * 4 suits)
        /// </summary>
        public const int NumberOfCardsInFullDeck = 52;

        /// <summary>
        /// An array containing all the cards in the deck.
        /// </summary>
        private Card[] cards;

        /// <summary>
        /// Prevents a default instance of the <see cref="Deck"/> class from being created.
        /// </summary>
        private Deck()
        {
        }

        /// <summary>
        /// Gets a full, shuffled deck.
        /// </summary>
        /// <value>A full, shuffled deck.</value>
        public static Deck FullShuffledDeck
        {
            get
            {
                var deck = new Deck();
                deck.CardsLeft = NumberOfCardsInFullDeck;
                deck.cards = new Card[NumberOfCardsInFullDeck];
                int index = 0;
                foreach (Suit suit in Enum.GetValues(typeof(Suit)))
                {
                    foreach (Rank cardValue in Enum.GetValues(typeof(Rank)))
                    {
                        deck.cards[index++] = new Card(cardValue, suit);
                    }
                }

                deck.Shuffle();

                return deck;
            }
        }

        /// <summary>
        /// Gets the number of cards left in the deck.
        /// </summary>
        /// <value>The number of cards left in the deck.</value>
        public int CardsLeft { get; private set; }

        /// <summary>
        /// Shuffles this deck.
        /// </summary>
        public void Shuffle()
        {
            Random random = new Random();
            for (int i = this.CardsLeft - 1; i >= 1; i--)
            {
                int j = random.Next(i + 1);
                Card temp = this.cards[i];
                this.cards[i] = this.cards[j];
                this.cards[j] = temp;
            }
        }

        /// <summary>
        /// Pops a card.
        /// </summary>
        /// <returns>The card the was popped</returns>
        public Card PopCard()
        {
            if (this.CardsLeft-- == 0)
            {
                throw new OutOfCardsException();
            }

            return this.cards[this.CardsLeft];
        }
    }
}